package com.sd.controller;

import com.sd.entity.*;
import com.sd.service.BusinessHospitalService;
import com.sd.service.StudentHospitalService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 学生报名(StudentHospital)表控制层
 *
 * @author makejava
 * @since 2021-05-17 16:42:05
 */
@Controller
@RequestMapping("student")
public class StudentHospitalController {
    /**
     * 服务对象
     */
    @Resource
    private StudentHospitalService studentHospitalService;
    /**
     * 服务对象
     */
    @Resource
    private BusinessHospitalService businessHospitalService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public StudentHospital selectOne(Integer id) {
        return this.studentHospitalService.queryById(id);
    }



    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 去显示所有商品的页面
     * @create time: 2021/4/22 9:18
     * @return
     */
    @RequestMapping("toShowStudentPage")
    public String toShowStudentPage(){

        return "showStudent";
    }
    @RequestMapping("toAddStudentPage")
    public ModelAndView toAddStudentPage(Integer businessId){
        ModelAndView mv = new ModelAndView();
        //获取活动id
        BusinessHospital businessHospital = businessHospitalService.queryById(businessId);
        mv.addObject("businessHospital",businessHospital);
        //显示已经报名的人数
        //返回所有的商品条数
        Map<String, Long> map = studentHospitalService.getCountAlready(businessId);
        //从map集合中获取key为myCount的value值
        Long  countAlready= map.get("myCount");
        mv.addObject("countAlready",countAlready);
        mv.setViewName("addStudent");
        return mv;
    }

    @RequestMapping("findAllStudent")
    //返回json数据
    @ResponseBody
    public ReturnBean findAllStudent(int page, int limit,StudentHospital studentHospital){
        //page当前第几页
        //limit一页显示多少行
        int  offset=(page-1)*limit;
        //int offset 起始位置, int limit，长度，共几行，一页显示多少行
        List<StudentHospitalVo> StudentList = studentHospitalService.queryAllByLimit(offset, limit,studentHospital);
        ReturnBean table = new ReturnBean();
        table.setCode(0);
        //返回所有的商品条数
        Map<String, Long> map = studentHospitalService.getCount(studentHospital);
        //从map集合中获取key为myCount的value值
        Long myCount = map.get("myCount");
        table.setCount( myCount);
        table.setData(StudentList);
        return table;
    }

    @RequestMapping("saveStudent")
    @ResponseBody
    public Result saveStudent(StudentHospital student){
        Result result= new Result();
        try {
            student.setCreateTime(new Date());
            studentHospitalService.insert(student);
            result.setCode(0);
            result.setMsg("添加成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("添加失败");
        }
        return result;
    }
    @RequestMapping("saveStudentForm")
    public String saveStudentForm(StudentHospital student){
        student.setCreateTime(new Date());
        if (student.getPhone()==null||student.getPhone()==""){
            //判断非空
            return "addStudent";
        }
        try{
            studentHospitalService.insert(student);
            return "success";
        }catch (Exception e){
            return "error";
        }
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 修改学生
     * @create time: 2021/5/18 9:10
     * @return
     */
    @RequestMapping("updateStudent")
    @ResponseBody
    public Result updateStudent( StudentHospital Student){
        Result result= new Result();
        try {
            studentHospitalService.update(Student);
            result.setCode(0);
            result.setMsg("修改成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("修改失败");
        }
        return result;
    }
    @RequestMapping("deleteStudentById")
    @ResponseBody
    public Result deleteStudentById( int id){
        Result result= new Result();
        try {
            studentHospitalService.deleteById(id);
            result.setCode(0);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("删除失败");
        }
        return result;
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 按照id修改学生
     * @create time: 2021/4/29 16:17
     * @return
     */
    @RequestMapping("deleteStudentByIds")
    @ResponseBody
    /**RequestBody将json字符串转换成java对象*/
    public Result deleteStudentByIds( @RequestBody List<StudentHospital> studentList){
        Result returnEntity =new Result();
        boolean b = studentHospitalService.deleteStudentByIds(studentList);
        if(b){
            returnEntity.setCode(0);
            returnEntity.setMsg("批量删除成功");
        }else{
            returnEntity.setCode(1);
            returnEntity.setMsg("批量删除失败");
        }
        return returnEntity;
    }
}